classdef ziSpectrumData < EquipmentControl.ZI.ziData.ziData
    %ZISPECTRUMDATA Summary of this class goes here
    %   Detailed explanation goes here
    
    properties
        default_channelName
        channel_Name
    end
    
    methods
        function obj = ziSpectrumData(data, varargin)
            %ZISPECTRUMDATA Construct an instance of this class
            %   Detailed explanation goes here
            p = inputParser();
            p.addParameter('channel_name', 'demods', @(x) ismember(x, {'demods', 'pids'}) );
            p.parse(varargin{:});
            
            obj@EquipmentControl.ZI.ziData.ziData(data);
            obj.default_channelName = 'demods';
            obj.channel_Name = p.Results.channel_name; %obj.default_channelName;
        end
        
        function obj = setChannelName(obj, chName)
            obj.channel_Name = chName;
        end
        
        function data = getNodeData(obj, channelIndex, sampleName, varargin)
            p = inputParser;
            
            chTest = @(x) ismember(x, {'0', '1', '2', '3'});
            typeTest = @(x) ismember(x, {'PSD', 'PS'});
            
            p.addRequired('channelIndex', chTest);
            p.addRequired('sampleName');
            p.addParameter('sampleIndex', 1);
            p.addParameter('type', 'PSD', typeTest);
            
            p.parse(channelIndex, sampleName, varargin{:});
            
            sampleIndex = p.Results.sampleIndex;
            type = p.Results.type;
            

            if ischar(channelIndex)
                chIdx = str2double(channelIndex) + 1;
            else
                chIdx = channelIndex;
            end
            
            sa_name = lower(replace(sampleName, '.', '_'));
            
            data.label = ['demods/' channelIndex '/' sampleName];
            data.info = obj.getData(obj.channel_Name, chIdx, sa_name, sampleIndex, 'header');
            data.freq = obj.getFrequency(obj.channel_Name, chIdx, sa_name, sampleIndex);
            data.value = obj.getData(obj.channel_Name, chIdx, sa_name, sampleIndex, 'value');            
            if strcmp(type, 'PSD')
                data.value = data.value./ sqrt(data.info.gridcoldelta * data.info.nenbw);
            end
        end
        
        function freqArray = getFrequency(obj, channel_Name, chIdx, sampleName, sampleIndex)
            head_info = obj.getData(channel_Name, chIdx, sampleName, sampleIndex, 'header');
            freqArray = (double(0:(head_info.gridcols-1))*head_info.gridcoldelta).';
        end
        
        res = analyze(obj, channelIndex, sampleName, varargin)
    end
end

